package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Charge;
import com.huike.pojo.Register;
import com.huike.service.ChargeService;
import com.huike.service.CheckChargeService;
import com.huike.service.HospitalService;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("checkCharge")
public class CheckChargeController {

    @Autowired
    private CheckChargeService checkChargeService;

    @RequestMapping("getRegister")
    public ResultVO<Register> getRegister(String registerName,String id,
                                                  @RequestParam(value="pageNum",defaultValue="1") Integer pageNum,
                                                  @RequestParam(value = "pageSize" ,defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List list = checkChargeService.getRegister(registerName,id);
        PageInfo<Register> pageInfo= new PageInfo<>(list,5);
        return new ResultVO<>(pageInfo);
    }
    @RequestMapping("getByidRegister")
    public ResultVO<Register> getByidRegister(Integer id){
        return new ResultVO<>(checkChargeService.getByidRegister(id));
    }

    @RequestMapping("addCharge")
    public ResultVO<Void> addCharge(@RequestParam(value = "id",required = false)Integer id,
                                    @RequestParam(value = "charge",required = false) List<Integer> charge){
        int row = 0;
        for(Integer i:charge){
            checkChargeService.addCharge(i,id);
            row++;
        }
        if(row!=charge.size()){
            return new ResultVO<>(401,"有"+(charge.size()-row)+"行添加失败");
        }
        return new ResultVO<>(200,"添加成功");
    }

    @RequestMapping("infoRefisterCharge")
    public ResultVO<Charge> infoRefisterCharge(Integer hospitalId){
        List<Charge> list = checkChargeService.infoRefisterCharge(hospitalId);
        return new ResultVO<>(list);
    }

    @RequestMapping("delChargeHospital")
    public ResultVO<Charge> delChargeHospital(Integer id){
        Integer row = checkChargeService.delChargeHospital(id);
        if(row<=0){
            return new ResultVO<>(500,"删除时未知异常");
        }
        return new ResultVO<>(200,"删除成功");
    }

}
